﻿using Compass.Wasm.Shared.Categories;
using MaterialDesignThemes.Wpf;
using Prism.Mvvm;

namespace Compass.Wpf.ViewModels.Dialogs;

public class AddFinishGoodsCountViewModel : BindableBase, IDialogHostAware
{
    public readonly IEventAggregator _aggregator;
    public AddFinishGoodsCountViewModel(IContainerProvider provider)
    {
        _aggregator = provider.Resolve<IEventAggregator>();
        SaveCommand =new DelegateCommand(Save);
        CancelCommand=new DelegateCommand(Cancel);
    }
    public string DialogHostName { get; set; }
    public DelegateCommand SaveCommand { get; set; }
    public DelegateCommand CancelCommand { get; set; }

    #region 属性
    private FinishedGoodsCountDto finishedGoodsCountDto;
    public FinishedGoodsCountDto FinishedGoodsCountDto
    {
        get => finishedGoodsCountDto;
        set { finishedGoodsCountDto = value; RaisePropertyChanged(); }
    }
    private string[] sbus = null!;
    public string[] Sbus
    {
        get => sbus;
        set { sbus = value; RaisePropertyChanged(); }
    }
    private string[] warehouseLocations = null!;
    public string[] WarehouseLocations
    {
        get => warehouseLocations;
        set { warehouseLocations = value; RaisePropertyChanged(); }
    }
    #endregion

    private void Cancel()
    {
        if (DialogHost.IsDialogOpen(DialogHostName))
            //取消时只返回No，告知操作结束
            DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
    }

    private void Save()
    {
        //验证数据已填写
        if (string.IsNullOrWhiteSpace(FinishedGoodsCountDto.OdpNumber) ||
            FinishedGoodsCountDto.PalletQuantity==0)
        {
            _aggregator.SendMessage("请填写项目号，且托盘数量不为0");
            return;
        }

        if (!DialogHost.IsDialogOpen(DialogHostName)) return;
        DialogParameters param = new() { { "Value", FinishedGoodsCountDto } };
        //保存时传递参数
        DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK, param));
    }

    public void OnDialogOpen(IDialogParameters parameters)
    {
        if (!parameters.ContainsKey("Value")) Cancel();
        FinishedGoodsCountDto= parameters.GetValue<FinishedGoodsCountDto>("Value");
        Sbus = Enum.GetNames(typeof(Sbu_e));
        WarehouseLocations = Enum.GetNames(typeof(WarehouseLocation_e));
    }
}